package org.kl.bpm.web.conf;

import java.util.List;
import java.util.Map;

import javax.servlet.ServletRequest;
import javax.validation.Valid;

import org.apache.commons.lang3.StringUtils;
import org.kl.bf.entity.easyui.EasyuiGrid;
import org.kl.bf.web.base.BasicController;
import org.kl.bpm.entity.conf.BpmConfNotice;
import org.kl.bpm.service.conf.BpmConfNoticeService;
import org.kl.bpm.service.conf.BpmMailTemplateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import org.springside.modules.web.MediaTypes;
import org.springside.modules.web.Servlets;

/**
 * 流程节点邮件提醒配置
 * 
 * @Author hqm
 * @CreateTime 2014年5月27日 下午8:23:56
 */
@Controller
@RequestMapping(value = "/bpm/confNotice")
public class BpmConfNoticeController extends BasicController {
	@Autowired
	private BpmConfNoticeService bpmConfNoticeService;
	@Autowired
	private BpmMailTemplateService bpmMailTemplateService;

	@RequestMapping(method = RequestMethod.GET)
	public String list(@RequestParam(value = "bpmConfNodeId", defaultValue = "-1") String bpmConfNodeId, Model model) {
		model.addAttribute("bpmConfNodeId", bpmConfNodeId);
		model.addAttribute("mailTemplateList", bpmMailTemplateService.getAllBpmMailTemplate());
		return "bpm/bpmConfNoticeList";
	}

	// @RequiresPermissions("bpmConfNotice:list")
	@RequestMapping(value = "dataGrid", produces = MediaTypes.JAVASCRIPT_UTF_8)
	@ResponseBody
	public String dataGrid(@RequestParam(value = "pageNumber", defaultValue = "1") int pageNumber,
			@RequestParam(value = "sort", defaultValue = "type") String sortType,
			@RequestParam(value = "order", defaultValue = "asc") String orderType, Model model, ServletRequest request) {
		Map<String, Object> searchParams = Servlets.getParametersStartingWith(request, SEARCH_PREFIX);
		List<BpmConfNotice> bpmConfNotices = bpmConfNoticeService.searchAll(searchParams,sortType, orderType);
		EasyuiGrid grid = new EasyuiGrid();
		grid.setRows(bpmConfNotices);
		grid.setTotal((int) bpmConfNotices.size());
		String result = jsonMapper.toJson(grid);
		return result;
	}

	// @RequiresPermissions("bpmConfNotice:update")
	@RequestMapping(value = "update", method = RequestMethod.POST)
	@ResponseBody
	public String update(@Valid @ModelAttribute("bpmConfNotice") BpmConfNotice bpmConfNotice, BindingResult result,
			RedirectAttributes redirectAttributes) {
		if (bpmConfNotice.getBpmConfNode() == null || StringUtils.isBlank(bpmConfNotice.getBpmConfNode().getId())) {
			return ajaxMsg("编辑失败，节点标识获取失败，请您刷新页面重试", false, null);
		}
		if (result.hasErrors()) {
			return ajaxMsg(bindingMsg(result), false, null);
		}
		bpmConfNoticeService.saveBpmConfNotice(bpmConfNotice);
		return ajaxMsg("编辑成功", true, null);
	}

	// @RequiresPermissions({ "bpmConfNotice:delete", "bpmConfNotice:update" })
	@RequestMapping(value = "delete/{id}", produces = MediaTypes.JAVASCRIPT_UTF_8)
	@ResponseBody
	public String delete(@PathVariable("id") String id, RedirectAttributes redirectAttributes) {
		bpmConfNoticeService.deleteBpmConfNotice(id);
		return ajaxMsg("删除成功", true, null);
	}

	/**
	 * 所有RequestMapping方法调用前的Model准备方法, 实现Struts2
	 * Preparable二次部分绑定的效果,先根据form的id从数据库查出Task对象,再把Form提交的内容绑定到该对象上。
	 * 因为仅update()方法的form中有id属性，因此仅在update时实际执行.
	 */
	@ModelAttribute
	public void getBpmConfNotice(@RequestParam(value = "id", defaultValue = "-1") String id, Model model) {
		if (!id.equals("-1")) {
			model.addAttribute("bpmConfNotice", bpmConfNoticeService.getBpmConfNotice(id));
		}
	}
}
